package top.suven.base.core.cat.dubbo;

import com.alibaba.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import top.suven.base.util.constants.GlobalConstants;
import top.suven.base.util.utilize.DateUtil;
import top.suven.base.util.utilize.UUIDGenerator;

/**
 * 全局日志跟踪类型
 *
 * @author vincentdeng
 */
public class GlobalLogbackThread {

   private static Logger logger = LoggerFactory.getLogger(GlobalLogbackThread.class);

    public static void setGlobalLogbackTraceId(){
        try{
            String traceId = RpcContext.getContext()
                    .getAttachment(GlobalConstants.LOGBACK_TRACE_ID);
            if(null == traceId || "".equals(traceId)){
                traceId =  MDC.get(GlobalConstants.LOGBACK_TRACE_ID);
            }
            if(null != traceId){
                MDC.put(GlobalConstants.LOGBACK_TRACE_ID,traceId);
                RpcContext.getContext().setAttachment(GlobalConstants.LOGBACK_TRACE_ID, traceId);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("Exception by setGlobalLogbackTraceId error[{}]",e);
        }
    }

    public static String getTraceIdByMDC(String module){

        try{
           String traceId =  MDC.get(GlobalConstants.LOGBACK_TRACE_ID);
           if(traceId != null){
               return traceId;
           }
           return initGlobalLogbackTraceId(module);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("Exception by setGlobalLogbackTraceId error[{}]",e);
        }
        return "";
    }

    public static String initGlobalLogbackTraceId(String module){
        if(module == null){
            return "";
        }
        String traceId = new StringBuilder("[")
                .append(module)
                .append("_")
                .append(DateUtil.getNowToNumFormat())
                .append("_")
                .append(UUIDGenerator.halfUUID())
                .append("]").toString();
        MDC.put(GlobalConstants.LOGBACK_TRACE_ID,traceId);
        logger.info("MDC server global logback info logback_trace_id ");
        return traceId;
    }

    public static void removeTraceId(){
        MDC.remove(GlobalConstants.LOGBACK_TRACE_ID);
    }



}


